from symbide import ThreadExecutionManager, SubprocessExecutionManager

import gtk
from widgetOrganizer import WidgetOrganizer


class MainToolbar(WidgetOrganizer, gtk.HBox):
    def __init__(self, ancestor):
        gtk.HBox.__init__(self)
        WidgetOrganizer.__init__(self, 'MainToolbar', ancestor)

        children = (Toolbar_File(self),
                    Toolbar_Execution(self),)
        for child in children:
            self.addChild(child)
            self.pack_start(child)


class Toolbar_File(WidgetOrganizer, gtk.HandleBox):
    def __init__(self, ancestor):
        gtk.HandleBox.__init__(self)
        WidgetOrganizer.__init__(self, 'Toolbar_File', ancestor)
        
        self.toolbar = gtk.Toolbar()
        self.toolbar.show()
        self.add(self.toolbar)
        
        buttons = (('new', gtk.STOCK_NEW),
                   ('open', gtk.STOCK_OPEN),
                   ('save', gtk.STOCK_SAVE),)
        
        for label, stock in buttons:
            button = gtk.ToolButton(stock)
            button.show()
            item = gtk.ToolItem()
            item.add(button)
            item.show()
            button.connect('clicked',eval('self._event_'+label))
            self.toolbar.insert(item, -1)
        
    def _event_new(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').addWorksheet()
        
    def _event_open(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').openSheet()
        
    def _event_save(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').saveSheet()
 
        
class Toolbar_Execution(WidgetOrganizer, gtk.HandleBox):
    def __init__(self, ancestor):
        gtk.HandleBox.__init__(self)
        WidgetOrganizer.__init__(self, 'Toolbar_File', ancestor)
        
        self.toolbar = gtk.Toolbar()
        self.toolbar.show()
        self.add(self.toolbar)
        
        buttons = (('execute_all', gtk.STOCK_EXECUTE),
                   ('terminate_all', gtk.STOCK_STOP),)
        
        for label, stock in buttons:
            button = gtk.ToolButton(stock)
            button.show()
            item = gtk.ToolItem()
            item.add(button)
            item.show()
            button.connect('clicked',eval('self._event_'+label))
            self.toolbar.insert(item, -1)
        
    def _event_terminate_all(self, widget):
        ThreadExecutionManager.terminate_all()
        SubprocessExecutionManager.terminate_all()
        
    def _event_execute_all(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').execute_current_sheet()
        